<?php

namespace common\request;

use cm;
use Yii;

class Request
{
    function get($key, $rule = array(), $json = true)
    {
        $notNull = true;
        $notEmpty = true;
        foreach ($rule as $k => $v) {
            if (is_numeric($k))
                switch ($v) {
                    case "null":
                        $notNull = false;
                        break;
                    case "empty":
                        $notEmpty = false;
                        break;
                }
        }

        $default = isset($rule['default']) ? $rule['default'] : '';
        $reg = isset($rule['reg']) ? $rule['reg'] : '';


        if (($notNull || $notEmpty) && !isset($_GET[$key]))
            Cm::$res->error("param {$key} not null !", [], $json);

        $value = isset($_GET[$key]) ? $_GET[$key] : $default;
        if ($notEmpty && !strlen($value))
            Cm::$res->error("param {$key} not empty !", [], $json);

        if (!empty($reg) && $notEmpty) {
            if (!preg_match($reg, $value, $arr))
                Cm::$res->error("param {$key} does not meet the rules !", [], $json);
        }
        $this->param_check($value);
        return $value == "null" ? "" : $value;
    }

    function post($key, $rule = array())
    {
        $title = isset($rule['title']) ? $rule['title'] : $key;
        $msgNull = isset($rule['msg_null']) ? $rule['msg_null'] : "请填写{$title}!";
        $msgEmpty = isset($rule['msg_empty']) ? $rule['msg_empty'] : "请填写{$title}!";
        $msgReg = isset($rule['msg_reg']) ? $rule['msg_reg'] : "请填写正确的{$title}!";


        $notNull = true;
        $notEmpty = true;
        foreach ($rule as $k => $v) {
            if (is_numeric($k))
                switch ($v) {
                    case "null":
                        $notNull = false;
                        break;
                    case "empty":
                        $notEmpty = false;
                        break;
                }
        }

        $default = isset($rule['default']) ? $rule['default'] : '';
        $reg = isset($rule['reg']) ? $rule['reg'] : '';


        if (($notNull || $notEmpty) && !isset($_POST[$key]))
            Cm::$res->error($msgNull);

        $value = isset($_POST[$key]) ? $_POST[$key] : $default;
        if ($notEmpty && !strlen($value))
            Cm::$res->error($msgEmpty);

        if (!empty($reg) && $notEmpty) {
            if (!preg_match($reg, $value, $arr))
                Cm::$res->error($msgReg);
        }

        $this->param_check($value);
        return $value == "null" ? "" : $value;
    }

    function param($key, $rule = array())
    {
        if (Cm::$req->isPost())
            return $this->post($key, $rule);

        return $this->get($key, $rule);
    }

    function isPost()
    {
        return Yii::$app->request->method == 'POST' ? true : false;
    }

    private function param_check(&$post)
    {
        //if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开
        //$post = addslashes($post); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤

        //$post = str_replace("_", "\_", $post); // 把 '_'过滤掉
        //$post = str_replace("%", "\%", $post); // 把' % '过滤掉
        //$post = nl2br($post); // 回车转换
        //$post = htmlspecialchars($post); // html标记转换

        $post = str_replace("'", "\'", $post);
        //$post = str_replace('"', '\"', $post);
        return $post;
    }
}